

package com.zy.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zy.entity.Branch;
import com.zy.mapper.BranchMapper;
import com.zy.service.BranchService;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class BranchServiceImpl implements BranchService {
    @Resource
    BranchMapper branchMapper;

    public BranchServiceImpl() {
    }

    @Override
    public Branch getById(Integer id) {
        return (Branch)this.branchMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(Branch branch) {
        this.branchMapper.insert(branch);
    }

    @Override
    public void updateById(Branch branch) {
        this.branchMapper.updateByPrimaryKey(branch);
    }

    @Override
    public PageInfo<Branch> page(Branch branch, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return new PageInfo(this.branchMapper.page(branch));
    }

    @Override
    public void delete(Integer id) {
        this.branchMapper.deleteByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void status(Integer id) {
        Branch branch1 = branchMapper.selectByPrimaryKey(id);
        if(branch1.getBrStatus()==1){
            branchMapper.insertHistory(branch1.getBrId(),new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }
        branch1.setBrStatus(branch1.getBrStatus()==0?1:0);
        this.branchMapper.updateByPrimaryKey(branch1);
    }

    @Override
    public PageInfo<Branch> pageHistory(Branch branch, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return new PageInfo(this.branchMapper.pageHistory(branch));
    }
}
